$(function () {
    addModal = false;
    editModal = false;
    openModal = false;
    var currentAlter;
	var lineMove = false;
	var currTh = null;
	//列移动
	$(".main").bind("selectstart", function() { return !lineMove; });
     $(".main").bind("mousemove", function(event) {
         if (lineMove == true) {
             $("#line").css({ "left": event.clientX }).show();
         }
     });

     $("th").bind("mousemove", function(event) {
         var th = $(this);
        if (th.prevAll().length < 1) {
           return;
        }
         var left = th.offset().left;
		 console.log(th.offset());
         if (event.clientX - left < 4 || (th.width() - (event.clientX - left)) < 4) {
             th.css({ 'cursor': 'move' });
         }
         else {
             th.css({ 'cursor': 'default' });
         }
     });

     $("th").bind("mousedown", function(event) {
         var th = $(this);
         //if (th.prevAll().length <= 1 || th.nextAll().length < 1) {
             //return;
         //}
         var pos = th.offset();
         if (event.clientX - pos.left < 4 || (th.width() - (event.clientX - pos.left)) < 4) {
             var top = pos.top;
             var height=$("#table1").height();
             $("#line").css({ "height":height,"top": top,"left":event .clientX,"display":"" });
             lineMove = true;
             if (event.clientX - pos.left < th.width() / 2) {
                 currTh = th.prev();
             }
             else {
                 currTh = th;
             }
         }
     });
	 $(".main").bind("mouseup", function(event) {
         if (lineMove == true) {
             $("#line").hide();
             lineMove = false;
             var pos = currTh.offset();
             var index = currTh.prevAll().length;
             currTh.width(event.clientX - pos.left);
             currTh.parent().parent().find("tr").each(function() {
                 $(this).children().eq(index).width(event.clientX - pos.left);
             }); 
         }
     });
	//列移动
    $("#add").click(function () {
        addModal = true;
        $("#myModal").modal("show");
        $("#myModalLabel").text("");
        $("#myModalLabel").text("添加新信息");
    });
    $("#table1").on("dblclick","td:not(td:nth-of-type(1))",function(){
        var currentValue = $(this).text();
        var currentTd = $(this);
        currentTd.text("");
        var inputObj = $("<input type='text'>");
        inputObj.val(currentValue);
        currentTd.append(inputObj);
        inputObj.focus();
        inputObj.blur(function () {
            currentValue = inputObj.val();
            currentTd.html("");
            currentTd.text(currentValue);
        });
    });
    $("#edit").click(function () {
        var moveCheck = $("input[type='checkbox']:checked");
        if (moveCheck.length == 0) {
            alert("请选中要编辑的选项");
        }
        else if (moveCheck.length != 1) {
            alert("只能编辑一个");
        } else {
            editModal = true;
            $("#myModal").modal("show");
            $("#myModalLabel").text("");
            $("#myModalLabel").text("编辑新信息");
            currentAlter = moveCheck.parent().parent();
            var a = [];
            for (i = 0; i < 3; i++) {
                a[i] = currentAlter.children().eq(i + 1).text();
            }
            // console.log(a);
            for (i = 0; i < 3; i++) {
                $(".modal−bodyinput").eq(i).val(a[i]);
            }
        }
    });
    $("#commit").click(function () {
        if (addModal) {
            var a = [];
            for (i = 0; i < 3; i++) {
                a[i] = $(".modal−bodyinput").eq(i).val();
            }
            console.log(a);
            var newRow = "<tr><td><input type=\"checkbox\" /></td><td>"
                + a[0] + "</td><td>"
                + a[1] + "</td><td>"
                + a[2] + "</td></tr>";
            $("#table1 tbody tr:last").after(newRow);
            addModal = false;
        }
        if (editModal) {
            for (i = 0; i < 3; i++) {
                currentAlter.children().eq(i + 1).text($(".modal−bodyinput").eq(i).val());
            }
            edit = false;
        }
    });
    $("#delete").click(function () {
        var deleteCheck = $("input[type='checkbox']:checked");
        if (deleteCheck.length === 0) {
            alert("请勾选要删除的选项");
        }
        else {
            var deleteRow = deleteCheck.parent().parent();
            deleteRow.remove();
        }
    });
    $("#moveUp").click(function () {
        var moveCheck = $("input[type='checkbox']:checked");
        if (moveCheck.length != 1) {
            alert("只能移动一个");
        } else {
            var current = moveCheck.parent().parent();
            var prev = current.prev();
            if (prev.length == 0) {
                alert("已经到顶啦!");
            }
            else if (prev) {
                current.insertBefore(prev);
            }
        }
    });
    $("#moveDown").click(function () {
        var moveCheck = $("input[type='checkbox']:checked");
        if (moveCheck.length != 1) {
            alert("只能移动一个");
        } else {
            var current = moveCheck.parent().parent();
            var next = current.next();
            if (next.length == 0) {
                alert("已经到底啦!");
            }
            else if (next) {
                current.insertAfter(next);
            }
        }
    });
    $(document).keydown(function (event) {
        if (event.keyCode == 38) {
            $("#moveUp").click();
        }
        if (event.keyCode == 40) {
            $("#moveDown").click();
        }
    });
    $("#allCheck").click(function () {
        $(":checkbox").prop("checked", true);
    });
    $("#cancelCheck").click(function(){
        $(":checkbox").prop("checked", false);
    });
    $("#invertCheck").click(function () {
        $(":checkbox").each(function(){
            var isCheck=$(this).prop("checked")?false:true;
            $(this).prop("checked",isCheck);
        });
    });
});
// oddEvenCss = function () {
//     $("#table1 tbody tr").removeClass("even odd");
//     $("#table1 tbody tr:odd").addClass("odd");
//     $("#table1 tbody tr:even").addClass("even");
//     $("#table1 tbody tr").hover(function () {
//         $(this).addClass("hover");
//     }, function () {
//         $(this).removeClass("hover");
//     });
// }
// init = function () {
//     oddEvenCss();
// }